package inc.sneid.reminderSpring.services;

import inc.sneid.reminderSpring.domains.Deltas;
import inc.sneid.reminderSpring.exceptions.DeltasNotFoundException;
import inc.sneid.reminderSpring.repos.DeltasRepository;
import inc.sneid.reminderSpring.utils.TimestampUtils;
import jakarta.annotation.PostConstruct;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class DeltasService {

    @PostConstruct
    public void postConstruct(){
        Deltas deltas = new Deltas("default", TimestampUtils.fromDays(List.of(1, 7, 16, 35)));
        deltasRepository.save(deltas);
    }

    private final DeltasRepository deltasRepository;

    public DeltasService(DeltasRepository deltasRepository) {
        this.deltasRepository = deltasRepository;
    }

    public Deltas getByName(String name) throws DeltasNotFoundException {
        return deltasRepository.findById(name).orElseThrow(DeltasNotFoundException::new);
    }

    public Page<Deltas> getPage(Pageable pageable){
        return deltasRepository.findAll(pageable);
    }

    public Deltas save(Deltas deltas) {
        return deltasRepository.save(deltas);
    }

    public void deleteByName(String name) {
        deltasRepository.deleteById(name);
    }
}
